﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.Web;

namespace Hkwl.WorldExpo.Merchant.Common
{
    public class CommonMethod
    {
        /// <summary>
        /// 产生一个3位随机数
        /// </summary>
        /// <returns></returns>
        public static int GetRandom()
        {
            Random r = new Random();
            return r.Next(100, 999);
        }

        public static int IntConvert(object obj)
        {
            try
            {
                return Convert.ToInt32(obj);
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 截取制定字符串,汉字2字节
        /// </summary>
        /// <param name="str"></param>
        /// <param name="length">截得的长度</param>...
        /// <returns></returns>
        /// 只能是先算总长度
        public static string SubString(string stringToSub, int length, string stringToAdd)
        {

            //stringToSubChar = Convert.ToByte(stringToSub);

            Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);

            char[] stringChar = stringToSub.ToCharArray();

            char[] stringToAddChar = stringToAdd.ToCharArray();

            length = length - stringToAddChar.Length;

            StringBuilder sb = new StringBuilder();

            int nLength = 0;

            for (int i = 0; i < stringChar.Length; i++)
            {
                if (regex.IsMatch((stringChar[i]).ToString()))
                {
                    sb.Append(stringChar[i]);
                    nLength += 2;
                }
                else
                {
                    sb.Append(stringChar[i]);
                    nLength = nLength + 1;
                }

                if (nLength >= length)
                {
                    if (i + 1 < stringChar.Length)
                        sb.Append(stringToAdd);
                    break;
                }
            }
            return sb.ToString();
        }
        /// <summary>
        /// Html编码
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string HtmEncode(string s)
        {
            string rval;
            rval = s.Replace("<", "&lt;");
            rval = rval.Replace(">", "&gt;");
            rval = s.Replace("&", "&amp;");
            rval = rval.Replace(Convert.ToChar(13).ToString(), "<br>");
            rval = rval.Replace(Convert.ToChar(34).ToString(), "&quot;");
            rval = rval.Replace(Convert.ToChar(32).ToString(), "&nbsp;");
            rval = rval.Replace(Convert.ToChar(9).ToString(), "	");
            return rval;
        }


        public static string HtmDecode(string s)
        {
            string rval;
            rval = s.Replace("&lt;", "<");
            rval = rval.Replace("&gt;", ">");
            rval = s.Replace("&amp;", "&");
            rval = rval.Replace("<br>", Convert.ToChar(13).ToString());
            rval = rval.Replace("&quot;", Convert.ToChar(34).ToString());
            rval = rval.Replace("&nbsp;", Convert.ToChar(32).ToString());
            rval = rval.Replace("	", Convert.ToChar(9).ToString());
            return rval;
        }

        public static string HtmToolTip(string s)
        {
            string rval;
            rval = s.Replace("&lt;", "<");
            rval = rval.Replace("&gt;", ">");
            rval = s.Replace("&amp;", "&");
            rval = rval.Replace("<br>", Convert.ToChar(13).ToString());
            rval = rval.Replace("&quot;", Convert.ToChar(34).ToString());
            rval = rval.Replace("&nbsp;", Convert.ToChar(32).ToString());
            rval = rval.Replace("	", Convert.ToChar(9).ToString());

            rval = System.Text.RegularExpressions.Regex.Replace(rval, "<[^>]+>", "");
            rval = System.Text.RegularExpressions.Regex.Replace(rval, "<[^>]*>", "");

            return rval;
        }

        public static bool GetModel(string strUrl, out string Result)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
                request.MaximumAutomaticRedirections = 4;
                request.MaximumResponseHeadersLength = 4;
                request.Credentials = CredentialCache.DefaultCredentials;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                System.IO.Stream receiveStream = response.GetResponseStream();
                System.IO.StreamReader readStream = new System.IO.StreamReader(receiveStream, Encoding.UTF8);
                Result = readStream.ReadToEnd();
                response.Close();
                readStream.Close();
                return true;
            }
            catch
            {
                //Log.ErrLog(ex);
                Result = "";
                return false;
            }
        }

        /// <summary>
        /// 返回当前日期的格式
        /// eg:20080801154654
        /// </summary>
        /// <returns></returns>
        public static string GetDateTimeStrings()
        {
            string strY = DateTime.Now.Year.ToString();
            string strM = DateTime.Now.Month.ToString().PadLeft(2, '0');
            string strD = DateTime.Now.Day.ToString().PadLeft(2, '0');
            string strH = DateTime.Now.Hour.ToString().PadLeft(2, '0');
            string strm = DateTime.Now.Minute.ToString().PadLeft(2, '0');
            string strS = DateTime.Now.Second.ToString().PadLeft(2, '0');

            string str = strY + strM + strD + strH + strm + strS;

            return str;
        }

        /// <summary>
        /// 记录cookie操作
        /// </summary>
        /// <param name="CookieValue">需要传送的值</param>
        public static HttpCookie CookieNewOperator(string CookieValue, string CookieName)
        {
            //cookie的植入操作
            //每次都创建同名的cookie覆盖原有cookie
            HttpCookie Cookie = new HttpCookie(CookieName);
            Cookie.Value = CookieValue;//此处cookie的值为要记录的内容
            Cookie.Expires = DateTime.Now.AddDays(1);
            return Cookie;
        }

        public static HttpCookie CookieOperator(HttpCookie CookieStr, string CookieValue)
        {
            //cookie的植入操作
            //每次都创建同名的cookie覆盖原有cookie
            HttpCookie Cookie = CookieStr;
            Cookie.Value = CookieValue;
            Cookie.Expires = DateTime.Now.AddDays(1);
            return Cookie;
        }
    }
}
